﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.DigitalOutput;

namespace MINERP.DigitalOutput.AutoDistribute
{
    public partial class FrmAuto : Form
    {
        //属性
        public FrmDigitalMain2 frm;
        public int CurrentTechId = 0;                           //当前流程
        public List<DigitalOutputBLL.STPrinter> ListPrinter = new List<DigitalOutputBLL.STPrinter>();             //当前可用的打印机集合
        //参数
        private DigitalOutputBLL bll = new DigitalOutputBLL();
        bool IsDistributing = false;                //是否正在分配
        public FrmAuto()
        {
            InitializeComponent();
        }

        private void FrmAuto_Load(object sender, EventArgs e)
        {
            MessageBox.Show(ListPrinter.Count.ToString());
        }

        //timer自动执行分配
        private void timerAuto_Tick(object sender, EventArgs e)
        {
            try
            {
                if (IsDistributing == false)
                {
                    IsDistributing = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确定", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //开始自动分配
        private void btnStart_Click(object sender, EventArgs e)
        {
            progressBar1.Style = ProgressBarStyle.Marquee;
            progressBar1.MarqueeAnimationSpeed = 100;
            progressBar1.Enabled = true;
            btnStart.Enabled = false;
            btnStop.Enabled = true;
            AutoDistribute();
        }
        //停止自动分配
        private void btnStop_Click(object sender, EventArgs e)
        {
            progressBar1.MarqueeAnimationSpeed = 0;
            progressBar1.Enabled = false;
            btnStart.Enabled = true;
            btnStop.Enabled = false;
        }

        private void AutoDistribute()
        {
            try
            {
                List<string> ErrList = new List<string>(); 
                DataTable dt = bll.GetPhotoList(false, CurrentTechId, 0,out ErrList);               //未分配打印机的所有照片
                if (ErrList != null || ErrList.Count != 0)
            {
                string errString = "";
                foreach (string s in ErrList)
                {
                    errString += s;
                }
                MessageBox.Show("以下照片尺寸为0请确认！[格式(产品号，照片号)]+errString", "确定", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }     
                frm.SetPhotoListDataSource(dt);
                //设置所有打印机的任务量
                foreach (DigitalOutputBLL.STPrinter p in ListPrinter)
                {
                    p.Task = bll.GetTask(p.PrinterID);
                }
            }
            catch
            { 
                
            }
        }
    }
}
